package com.hq.fxh.No0_ChengQu.Activity;

import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.StatesBarUtils;


/**
 * @author 薛
 * @ClassName BaseActivity
 * @Description : activity基类
 */
public abstract class BaseActivity extends AppCompatActivity {
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatesBarUtils.setWindowToWhite(this);
        initActivity(savedInstanceState);
    }

    public void setSysColor(int color, boolean status) {
        if (status) {
            StatesBarUtils.setWindowColor(this, color);

        } else {
            StatesBarUtils.setWindowToWhite(this);

        }
    }

    /**
     * 初始化 activity
     *
     * @param savedInstanceState
     */
    private void initActivity(Bundle savedInstanceState) {
        onBeforeSetContentLayout();
        View view = getLayoutView();
        if (view == null) {
            setContentView(getLayoutId());
        } else {
            setContentView(view);
        }

        long start = System.currentTimeMillis();
        init(savedInstanceState);
        try {
            initView();
            LogUtils.w(String.format("%s-findView：%s毫秒", getLocalClassName(),
                    (System.currentTimeMillis() - start)));
            start = System.currentTimeMillis();
            initData();
            LogUtils.w(String.format("%s-initData耗时：%s毫秒", getLocalClassName(),
                    (System.currentTimeMillis() - start)));
        } catch (Exception ex) {
            LogUtils.e(ex.getMessage());
        }
    }


    protected abstract void initView();

    protected abstract void initData();


    /**
     * 在setContentLayout之前执行逻辑
     */
    protected void onBeforeSetContentLayout() {
    }

    protected void init(Bundle savedInstanceState) {
    }

    /**
     * 界面布局文件
     *
     * @return
     */
    protected int getLayoutId() {
        return 0;
    }

    protected View getLayoutView() {
        return null;
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;

    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onRestart() {
        super.onRestart();

    }

    public void showProgressDialog(String message) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
        }
        progressDialog.setMessage(message);
        progressDialog.show();
    }

    public void dissProgressDialoig() {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
            afterProgressDismiss();
        }
    }

    /**
     * 加载对话框取消 ,可处理取消请求操作
     */
    protected void afterProgressDismiss() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
